/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author Jin Young Hur(hurims@gmail.com)
 */

var url;
var tags;
var repositoryUrl;
var classSpecificUrl;

(function() {

  url  = window.location.href;
  
  repositoryUrl = url.replace(/(.*)\+(.*)/, '$1');

  var xhr = new XMLHttpRequest();
  xhr.open("GET", repositoryUrl, true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {

      getTags(xhr.responseText);

      getClassSpecificUrl();

      createAndAppendTagsOverlay();

      document.addEventListener('keydown', function(event) {
        if(event.keyCode == '1'.charCodeAt(0)) {
          toggleTags();
        }
      });

    }
  }
  xhr.send();

})();

function getTags(htmlString) {
  var tempDiv = document.createElement('div');
  tempDiv.innerHTML = htmlString.replace(/<script(.|\s)*?\/script>/g, '');

  // tempDiv now has a DOM structure:
  tags = ['refs/heads/master'];
  var tempTags = tempDiv.getElementsByClassName("repository-tags")[0].getElementsByTagName("a");
  for (var i = 0; i < tempTags.length; i++) {
    tags.push(tempTags[i].text);
  };
}

function getClassSpecificUrl() {
  for (var i = 0; i < tags.length; i++) {
    var tag = tags[i];
    var foundIdx = url.toLowerCase().indexOf(tag.toLowerCase());
    if (foundIdx != -1) {
      classSpecificUrl = url.substring(foundIdx + tag.length);
    }
  }
}

function createAndAppendTagsOverlay() {
  overlay = document.createElement('div');
  overlay.setAttribute('id', 'overlay');
  appendNode = document.createElement('div');
  overlay.appendChild(appendNode);

  var appendContent = ['<h3>Tags</h3>'];
  appendContent.push('<ul>');

  for (var i = 0; i < tags.length; i++) {
    var tag = tags[i];
    appendContent.push([
        '<li>',
        '<a href="',
        repositoryUrl,
        '+/',
        tag,
        classSpecificUrl,
        '">',
        tag,
        '</a>',
        '</li>'
    ].join(''));
  }
  appendContent.push('</ul>');

  appendNode.innerHTML = appendContent.join('');

  document
    .getElementsByTagName('body')[0]
    .appendChild(overlay);

  var styles = '#overlay {visibility: hidden; position: absolute; left: 0px; top: 0px; width:100%; height:100%; z-index: 1000;background-image:url(background-trans.png);}';
  styles += ' #overlay div { width:300px; margin: 100px auto; background-color: #fff; border:1px solid #000; padding:15px; }';
  appendStyle(styles);
}

function appendStyle(styles) {
  var css = document.createElement('style');
  css.type = 'text/css';

  if (css.styleSheet) css.styleSheet.cssText = styles;
  else css.appendChild(document.createTextNode(styles));

  document.getElementsByTagName("head")[0].appendChild(css);
}

function toggleTags() {
  el = document.getElementById("overlay");
  el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}